package com.travel.photos.ui.travel;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.travel.R;
import com.travel.photos.bean.TraveResBean;
import com.travel.photos.image.ImageHepler;
import com.travel.photos.ui.otheruser.OtherUserActivity;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by meidou on 2016/10/19.
 */
public class TravelAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_ONE = 0;//head
    private static final int TYPE_TWO = 1; //recycleview
    private static final int TYPE_THREE = 2;//下拉加载更多;
    private Context context;//上下文
    private List<TraveResBean> totailist;

    public TravelAdapter(Context context, List<TraveResBean> totailist) {
        this.context = context;
        this.totailist = totailist;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ONE) {
            HeadViewHolder holder=new HeadViewHolder(LayoutInflater.from(context)
                    .inflate(R.layout.photo_travel_re, parent, false));
            return holder;
        } else if (viewType == TYPE_TWO) {
            MyViewHolder holder = new MyViewHolder(LayoutInflater.from(context)
                    .inflate(R.layout.item_photo_tracel_cycle, parent, false));
            return holder;
        } else if (viewType == TYPE_THREE) {
            FootViewHolder footholder=new FootViewHolder(LayoutInflater.from(context)
                    .inflate(R.layout.photo_recycler_load_more,parent,false));
            return footholder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if(holder instanceof HeadViewHolder){//

        }else if(holder instanceof MyViewHolder){//
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            myViewHolder.mPhotoFoucsItemText.setText(totailist.get(position-1).getName());
            if (totailist.get(position-1).getDesc() != null) {
                myViewHolder.mPhotoFoucsItemText2.setText(totailist.get(position-1).getDesc());
                myViewHolder.mPhotoFoucsItemText2.setVisibility(View.VISIBLE);
            }
            ImageHepler.showImage(context,totailist.get(position-1).getAvatar(),myViewHolder.mPhotoFoucsItemImage,true);
            myViewHolder.mPhotoFoucsItemImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, OtherUserActivity.class);
                    intent.putExtra("userId",totailist.get(position).getId());
                    intent.putExtra("name",totailist.get(position).getNickName());
                    intent.putExtra("image",totailist.get(position).getAvatar());
                    context.startActivity(intent);
                }
            });
            ImageHepler.showImage(context,totailist.get(position-1).getList().get(0).getPath(),myViewHolder.mPhotoFoucsItemImageone,false);
            ImageHepler.showImage(context,totailist.get(position-1).getList().get(1).getPath(),myViewHolder.mPhotoFoucsItemImagetwo,false);
            ImageHepler.showImage(context,totailist.get(position-1).getList().get(2).getPath(),myViewHolder.mPhotoFoucsItemImagethree,false);
            ImageHepler.showImage(context,totailist.get(position-1).getList().get(3).getPath(),myViewHolder.mPhotoFoucsItemImagefour,false);
        }else if(holder instanceof FootViewHolder){
            ((FootViewHolder) holder).tv_jiazai.setText("上拉加载更多...");
        }
    }

    @Override
    public int getItemCount() {
        return totailist.size()+2;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.photo_foucs_item_text)
        TextView mPhotoFoucsItemText;
        @Bind(R.id.photo_foucs_item_text2)
        TextView mPhotoFoucsItemText2;
        @Bind(R.id.photo_foucs_item_image)
        ImageView mPhotoFoucsItemImage;
        @Bind(R.id.photo_foucs_item_imageone)
        ImageView mPhotoFoucsItemImageone;
        @Bind(R.id.photo_foucs_item_imagetwo)
        ImageView mPhotoFoucsItemImagetwo;
        @Bind(R.id.photo_foucs_item_imagethree)
        ImageView mPhotoFoucsItemImagethree;
        @Bind(R.id.photo_foucs_item_imagefour)
        ImageView mPhotoFoucsItemImagefour;
        public MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    class HeadViewHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.travel_text)
        TextView mTravelText;

        HeadViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
    //下拉加载更多布局
    class FootViewHolder extends RecyclerView.ViewHolder{
        TextView tv_jiazai;
        ProgressBar iv_jiazai;
        public FootViewHolder(View itemView) {
            super(itemView);
            tv_jiazai= (TextView)itemView.findViewById(R.id.tv_jiazai);
            iv_jiazai= (ProgressBar)itemView.findViewById(R.id.iv_jiazai);
        }
    }
    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_ONE;//
        }
        if (position + 1 == getItemCount()) {
            return TYPE_THREE;//
        }
        else {
            return TYPE_TWO;//
        }
    }
}
